import React, { useState } from 'react';
import { Form, Button, Input, Modal } from 'antd';

import { GetTokenType } from '../data.d';

export interface DeleteFormProps {
  onCancel: (flag?: boolean, formVals?: GetTokenType) => void;
  onSubmit: (values: GetTokenType) => void;
  deleteModalVisible: boolean;
  values: GetTokenType;
}
const FormItem = Form.Item;


const formLayout = {
  labelCol: { span: 7 },
  wrapperCol: { span: 13 },
};

const DeleteForm: React.FC<DeleteFormProps> = (props) => {
  const [formValues, setFormValues] = useState<GetTokenType>({
    id: props.values.id,
    value: props.values.value,
    name: props.values.name,
  });

  const [form] = Form.useForm();

  const {
    onSubmit: handleDelete,
    onCancel: handleDeleteModalVisible,
    deleteModalVisible,
    values,
  } = props;

  const handleRealDelete = async () => {
    const fieldsValue = await form.validateFields();
    setFormValues({ ...formValues, ...fieldsValue });
    handleDelete({ ...formValues, ...fieldsValue });
  };

  const renderFooter = () => {
    return (
      <>
        <Button onClick={() => handleDeleteModalVisible(false, values)}>取消</Button>
        <Button type="primary" onClick={() => handleRealDelete()}>删除</Button>
      </>
    );
  };

  return (
    <Modal
      width={640}
      bodyStyle={{ padding: '32px 40px 48px' }}
      destroyOnClose
      title="删除token"
      visible={deleteModalVisible}
      footer={renderFooter()}
      onCancel={() => handleDeleteModalVisible()}
    >
      <Form
        {...formLayout}
        form={form}
        initialValues={{
          id: formValues.id,
          value: formValues.value,
          name: formValues.name
        }}
      >
        <FormItem
          name="id"
          label="id"
        >
          <Input placeholder="请输入" disabled={true} />
        </FormItem>
        <FormItem
          name="name"
          label="name"
        >
          <Input placeholder="请输入" disabled={true} />
        </FormItem>
        <FormItem
          name="value"
          label="token"
        >
          <Input placeholder="请输入" disabled={true} />
        </FormItem>
      </Form>
    </Modal>
  );
};

export default DeleteForm;
